<template>
  <el-dialog v-model="visible" :title="!dataForm.purchaseAnnouncementId ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="purchaseProjectId" label="采购项目">
        <el-select :disabled="true" v-model="dataForm.purchaseProjectId" single placeholder="请选则采购项目">
          <el-option v-for="role in purchaseProjectList" :key="role.purchaseProjectId" :label="role.purchaseProjectName" :value="role.purchaseProjectId"></el-option>
        </el-select>
      </el-form-item>
          <el-form-item label="公告名称" prop="announcementName">
        <el-input :disabled="true" v-model="dataForm.announcementName" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="公告性质" prop="announcementNature">
            <ren-radio-group :disabled="true" v-model="dataForm.announcementNature" dict-type="announcement_nature"></ren-radio-group>
      </el-form-item>
          <el-form-item label="报名开始时间" prop="applyStartTime">
        <el-input :disabled="true" v-model="dataForm.applyStartTime" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="报名结束时间" prop="applyEndTime">
        <el-input :disabled="true" v-model="dataForm.applyEndTime" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="公告状态" prop="isPublish">
            <ren-radio-group :disabled="true" v-model="dataForm.isPublish" dict-type="announcement_status"></ren-radio-group>
      </el-form-item>
          <el-form-item label="公告发布时间" prop="publishTime">
        <el-input :disabled="true" v-model="dataForm.publishTime" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="公告详情" prop="fileList">
        <div v-for="(value, key, index, url) in fileList" :key="key" style="display: flex">
          <div style="margin-right: 10px;">{{ value.fileName }} </div>
          <div>
            <el-button type="primary" link @click="doDownload(url, value.fileName)">下载</el-button>
          </div>
        </div>
      </el-form-item>
          <el-form-item label="项目创建人" prop="createUser">
        <el-input :disabled="true" v-model="dataForm.createUser" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="项目创建时间" prop="createTime">
        <el-input :disabled="true" v-model="dataForm.createTime" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="最后更新人" prop="updateUser">
        <el-input :disabled="true" v-model="dataForm.updateUser" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="最后更新时间" prop="updateTime">
        <el-input :disabled="true" v-model="dataForm.updateTime" placeholder=""></el-input>
      </el-form-item>
      </el-form>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";

const fileList = ref<any[]>([]);
const emit = defineEmits(["refreshDataList"]);
const purchaseProjectList = ref<any[]>([]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  purchaseAnnouncementId: '',  purchaseProjectId: '',  announcementName: '',  announcementNature: '',  applyStartTime: '',  applyEndTime: '',  isPublish: '',  publishTime: '',  createUser: '',  createTime: '',  updateUser: '',  updateTime: ''});

const init = (purchaseAnnouncementId?: number) => {
  visible.value = true;
  dataForm.purchaseAnnouncementId = "";
  Promise.all([getPurchaseProjectList()]).then(() => {
    if (purchaseAnnouncementId) {
      getInfo(purchaseAnnouncementId);
    }
  });
};

// 获取信息
const getInfo = (purchaseAnnouncementId: number) => {
  baseService.get("/purchaseannouncement/purchaseannouncement/" + purchaseAnnouncementId).then((res) => {
    Object.assign(dataForm, res.data);
    fileList.value = res.data.fileList;
  });
};

// 获取项目列表
const getPurchaseProjectList = () => {
  return baseService.get("/purchaseproject/purchaseproject/list").then((res) => {
    purchaseProjectList.value = res.data;
  });
};

defineExpose({
  init
});

const doDownload = (url, fileName) => {
      // this.getBlob(url).then(res =>{ //url:文件在oss上的地址
      //     this.saveAs(res,fileName) // filename:文件名，可自定义
      // })
      console.log( fileName);
      let link = document.createElement("a");
      link.href = url;
      link.setAttribute("download", fileName);
      link.click();
      link.remove();
    }
</script>
